package com.xtck.ha.fault.framework.utils;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> {
	/**
     * 请求结果，500为失败，200为成功
     */
    private Integer code=200;

    /**
     * 请求结果信息
     */
    private String message;

    /**
     * 请求返回的数据对象，也将被转为json格式
     */
    private T data;

    public static <T> JsonResult<T> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setData(data);
        return result;
    }
    public static <T> JsonResult<T> ok(String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setData(null);
        result.setMessage(message);
        return result;
    }
    public static <T> JsonResult<T> ok(T data,String message) {
        JsonResult<T> result = ok(data);
        result.setMessage(message);
        return result;
    }
    public static <T> JsonResult<T> error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.toString());
    }

    public static <T> JsonResult<T> error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> JsonResult<T> error(HttpStatus errorCode) {
        return error(errorCode.value(), errorCode.toString());
    }

    public static <T> JsonResult<T> error(int code, String msg) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
}
